package com.chuanyang.driverh.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.view.View;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.mainstart.LoginActivity;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.ErrorEvent;
import com.chuanyang.driverh.utils.eventbus.ExitAppEvent;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.io.Serializable;

import de.greenrobot.event.EventBus;


/**基础Activity 所有的Activity都继承此类*/
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener{
	protected Activity activityContext; //使用作为Context对象
	@Override
	public void onClick(View v) {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activityContext=this;
		registerEventBus();
	}

	/**
	 * 退出应用
	 */
	public void onEventMainThread(ExitAppEvent exitApp) {
		if (exitApp.isExit() && exitApp.isLogin()) {
			ActivityUtil.startActivity(activityContext,new Intent(activityContext,LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			finish();
		} else if (exitApp.isExit()) {
			unRegisterEventBus();//这一句必须加上，不然虽然能退出，但报很多错误
			finish();//到这里，多个activity可以关闭掉程序了 但是进程仍然存在，因此加上了下边一句话，可以杀死进程
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}

	public void onEventMainThread(ErrorEvent event) {
		int status = event.getStatus();
		String message = event.getMsg();
		if (event.getContext().equals(activityContext)) {
			LogUtils.i("error_status:" + event.getStatus());
			LogUtils.i("error_msg:" + event.getMsg());
			onFailure(status, message);
		}
	}

	/**
	 * 网络请求或请求数据失败的信息提示
	 *
	 * @param status
	 * @param message
	 */
	protected void onFailure(int status, String message) {
		PrintUtil.toast(activityContext,message);
		if (status == ConstUtil.NO_LOGIN || status ==  ConstUtil.NO_TOKEN) {
			SharedPrefUtil.setLogined(false);
			ActivityUtil.startActivity(activityContext,new Intent(activityContext,LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			finish();
		}
	}

	/**
	 * 触摸Activity空白处关闭软键盘(适用于总布局不是ScrollView的情况)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		ActivityUtil.hindSoftInput(activityContext);
		return super.onTouchEvent(event);
	}
	
	/**
	 * 统一Activity后退时的动画
	 * @see FragmentActivity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.finish_enter, R.anim.finish_exit);
	}

	@Override
	protected void onDestroy() {
		MyHttpUtil.cancelCall(activityContext);
		unRegisterEventBus();
		super.onDestroy();
	}


	/**
	 * 当前界面打开软键盘 跳转新的界面的时候关闭关键盘
	 */
	@Override
	protected void onPause() {
		ActivityUtil.hindSoftInput(activityContext);
		super.onPause();
	}
	public void startActivity(Class<?> cls, Object obj) {
		Intent intent = new Intent(this, cls);
		if (obj != null) {
			intent.putExtra("data", (Serializable) obj);
		}
		startActivity(intent);

	}

	/**
	 * 注册EventBus通信组件
	 */
	protected void registerEventBus() {
		if (activityContext!=null){
			try {
				EventBus.getDefault().register(activityContext);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 取消注册EventBus通信组件
	 */
	protected void unRegisterEventBus() {
		EventBus.getDefault().unregister(activityContext);
	}
}
